package org.sgroup.imenu.bo;

import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

public class Category {
	// "cat_id","cat_name","parent_id","priority","cat_image","is_active"
	public int ID;
	public String Code;
	public String Name;
	public int ParentID;
	public int Priority;
	public String ImageFile;
	public String Description;
	public int IsActive;
	public List<Category> childs;
	public List<Item> items;
   public void AddChild (Category _cat){
	   if (childs ==null) childs = new ArrayList<Category>();
	   childs.add(_cat);
   }
   
   public ArrayList<Integer> GetItems(List<CatItem> _cItem) {
	   ArrayList<Integer> itemIDs= new ArrayList<Integer>(); 
	   for (CatItem cItem : _cItem) {
		   if (cItem.CategoryID == this.ID)
			   itemIDs.add(cItem.ItemID);
	   } 
	 return itemIDs;
}
   public void AddItem (Item _item){
	   if (items ==null) items = new ArrayList<Item>();
	   items.add(_item);
   }
	private String writeXml(List<Category> _cat) {
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "messages");
			serializer.attribute("", "number", String.valueOf(_cat.size()));
			for (Category cat : _cat) {
				serializer.startTag("", "Category");
				serializer.attribute("", "ID", String.valueOf(cat.ID));
				serializer.attribute("", "Code", cat.Code);
				serializer.attribute("", "Priority",
						String.valueOf(cat.Priority));
				serializer.attribute("", "Description", cat.Description);
				serializer.attribute("", "Image", cat.ImageFile);
				serializer.attribute("", "IsActive",
						String.valueOf(cat.IsActive));
				serializer.endTag("", "Category");

			}
			serializer.endTag("", "messages");
			serializer.endDocument();
			return writer.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	
}
